<template>
    <div class="productsbox">
        <div class="productslist" v-for="(item,index) in foodlist" :key="index">
            <div class="productslistone">
                <img :src="'../images/'+item.foodImg" @click="single(item.foodName)">
                <p><span>-30%</span><span class="el-icon-star-off"></span></p>
            </div>
            <p>{{item.foodName}}</p>
            <p>
              <span>1 UNIT</span> 
              <el-rate style="display: inline-block; margin-left: 10px;"
                v-model="value"
                disabled
                show-score
                text-color="#ff9900"
                score-template="{value}">
              </el-rate>
            </p>
            <h4>￥{{item.foodPrice}}</h4>
            <p class="car">
              <el-input-number size="mini" style="width:115px" v-model="item.num" :min="0" :max="10" ></el-input-number>
              <span>加入购物车</span>
            </p>
        </div>
        <div class="pagent">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total">
        </el-pagination>
        </div>
    </div>
</template>
<script>
import {selectfoodlistByCategory} from '../../api/api.js' 
export default {
    name:"shop1",
    data(){
        return{
            foodlist:[],
            foodcategorylist:{},
            value:1.5,
            inputval:"",
            pageNum: 1, // 当前页码
            pageSize: 10,   // 每页显示的数量
            total: 0,
        }
    },
    methods:{
        //处理currentPage的变化
        handleCurrentChange(pageNum){
          // alert(pageNum)
          this.pageNum = pageNum
          // this.getfoodlistbydesc()
          this.getcategorylist()//刷新列表
        },
        getcategorylist(){
            let that=this;
             const categorytype = "蔬菜";
            selectfoodlistByCategory(this.pageNum,this.pageSize,categorytype).then(res => {
              console.log(res);
              if (res.code!==null) {
                console.log("------");
                console.log(res.data);
                that.foodlist = res.data.records;
                that.total = res.data.total;
                that.foodlist = res.data.records.map(item => ({ ...item, num: 0 }));
              } else {
                console.log("请求失败:", res.msg);
              }
            }).catch(error => {
              console.log("错误信息:"+error);
            })
        },
        single(name){
           this.$router.push({ name: 'single', params: { name: name } });
        }
    },
    created(){
        this.getcategorylist();
    }
}
</script>
<style scoped>
.productsbox{
    float: left;
    width: 1250px;
    height: 800px;
    display: inline-block;
    margin: 20px 30px;
}
.productslist{
    width: 200px;
    height: 320px;
    float: left;
    padding: 15px;
    margin: 10px 15px 10px 0;
    border-radius: 15px;
}
.productslist:hover{
    box-shadow: 0px 21px 44px rgba(0, 0, 0, 0.08);
  }
  .productslist p{
    line-height: 20px;
    margin: 10px 0 0 0;
  }
  .productslistone{
      border-radius: 10px;
      width: 200px;
      height: 170px;
      margin: 0;
  }
  .productslistone p{
    float: left;
    position: relative;
    bottom: 170px;
    margin: 0;
    height: 0;
    line-height: 0px;
    margin: 0 10px;
  }
  .productslistone img{
    margin: 0 auto;
    width: 200px;
    height: 170px;
    border-radius: 15px;
  }
  .productslistone span:first-child{
    float: left;
    width: 50px;
    display: inline-block;
    height: 25px;
    line-height: 25px;
    color: white;
    background-color: #a3be4c;
    font-weight: bold;
    text-align: center;
    margin: 10px 5px;
    font-size: 12px;
    border-radius: 8px;
  }
  .productslistone span:last-child{
    width: 45px;
    height: 45px;
    display: inline-block;
    border-radius: 45px;
    line-height: 45px;
    border: #d2d4d4 1px solid;
    margin: 0 10px 0 60px;
  }
  .productslistone span:last-child:hover{
    background-color: #f03838;
    color: white;
  }
  .el-icon-star-off{
    font-size: 23px;
    font-weight: bold;
    text-align: center;
  }
  .productslist h4{
    font-size: 25px;
    margin: 0;
  }
  .productslist p:nth-child(3){
    color: #5f696a;
    font-size: 13px;
  }
  .car span{
    float: right;
    color: #5f696a;
    font-size: 12px;
    margin: 0 10px;
  }
  .pagent{
    float: left;
    width: 1250px;
    height: 50px;
    margin: 0 auto;
  }
  .el-pagination{
    margin: 0 auto; 
    width: 200px;
  }
</style>
